package org.xxx.epms.feedback.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
import org.xxx.epms.common.api.CommonPage;
import org.xxx.epms.common.api.CommonResult;
import org.xxx.epms.feedback.dto.GridCityDto;
import org.xxx.epms.feedback.feign.AqiMeasuredResultFeign;
import org.xxx.epms.feedback.service.AqiFeedbackService;
import org.xxx.epms.feedback.controller.blockHandler.FeedBackControllerBlockHandler;
import org.xxx.epms.feedback.dto.AqiFeedbackDetailDTO;
import org.xxx.epms.feedback.dto.AqiFeedbackParam;
import org.xxx.epms.feedback.dto.MonitorDto;
import org.xxx.epms.feedback.feign.MonitorFeign;
import org.xxx.epms.feedback.model.AqiFeedback;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/feedback")
public class AqiFeedbackController {

    @Autowired
    private AqiFeedbackService aqiFeedbackService;
    @Qualifier("org.xxx.epms.feedback.feign.MonitorFeign")
    @Autowired
    private MonitorFeign monitorFeign;
    @Qualifier("org.xxx.epms.feedback.feign.AqiMeasuredResultFeign")
    @Autowired
    private AqiMeasuredResultFeign aqiMeasuredResultFeign;

    @RequestMapping("/getFeedbackListWithMonitorInfo")
    @SentinelResource(value = "getFeedbackListWithMonitorInfoResource", blockHandler = "getFeedbackListWithMonitorInfoBlocklHandler",blockHandlerClass = FeedBackControllerBlockHandler.class)
    public String getFeedbackListWithMonitorInfo(@RequestParam(value = "monId") String monId) {
        System.out.println(monId);
        return aqiFeedbackService.getFeedbackListWithMonitorInfo(monId);
    }

    //获取反馈列表总页数
    @RequestMapping(value = "/getFeedbackListTotalPage",method = RequestMethod.POST )
    @ApiOperation(value = "获取反馈列表总页数", notes = "获取反馈列表总页数")
    public CommonResult<Integer> getFeedbackListTotalPage(@RequestBody AqiFeedbackParam aqiFeedbackParam,
                                                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        int totalPage = aqiFeedbackService.getTotalPage(aqiFeedbackParam, pageSize);
        return CommonResult.success(totalPage);
    };

    //分页获取反馈列表
    @RequestMapping(value = "/getFeedbackList",method = RequestMethod.POST )
    @ApiOperation(value = "分页获取反馈列表", notes = "分页获取反馈列表")
    public CommonResult<CommonPage<AqiFeedback>> getFeedbackList(@RequestBody AqiFeedbackParam aqiFeedbackParam,
                                                                 @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                                 @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){
        Page<AqiFeedback> page = aqiFeedbackService.getPageList(aqiFeedbackParam, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(page));
    };

    //分页获取带监督员的反馈列表
    @RequestMapping(value = "/getFeedbackListDetail",method = RequestMethod.POST )
    @SentinelResource(value = "getFeedbackListDetailResource", blockHandler = "getFeedbackListDetailBlocklHandler",blockHandlerClass = FeedBackControllerBlockHandler.class)
    @ApiOperation(value = "分页获取带监督员的反馈列表", notes = "分页获取带监督员的反馈列表")
    public CommonResult<CommonPage<AqiFeedbackDetailDTO>> getFeedbackListDetail(@RequestBody AqiFeedbackParam aqiFeedbackParam,
                                                                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        Page<AqiFeedback> feedbackPage = aqiFeedbackService.getPageList(aqiFeedbackParam, pageSize, pageNum);
        Page<AqiFeedbackDetailDTO> page = new Page<>();
        List<AqiFeedbackDetailDTO> feedbackList = new ArrayList<>();
        //遍历反馈列表，查询监督员信息
        for (AqiFeedback aqiFeedback : feedbackPage.getRecords()) {
            AqiFeedbackDetailDTO dto = new AqiFeedbackDetailDTO();
            dto.setAqiFeedback(aqiFeedback);
            MonitorDto monitorDto = monitorFeign.getMonitorByMonId(aqiFeedback.getMonId().toString());
            dto.setMonitor(monitorDto);
//            System.out.println(monitorDto.getMonId());
            if(aqiFeedback.getFbImg()!=null){
                dto.setImageUrl(aqiFeedback.getFbImg().split(AqiFeedback.PATH_SEPARATOR));
            }
            feedbackList.add(dto);
        }
        page.setRecords(feedbackList);
        return CommonResult.success(CommonPage.restPage(page));
    };

    @RequestMapping(value = "/createAqiFeedback",method = RequestMethod.POST )
    @ApiOperation(value = "创建反馈", notes = "创建反馈")
    public CommonResult<Integer> createAqiFeedback(@RequestBody AqiFeedback aqiFeedback) {
        aqiFeedback.setFbId(null);
        aqiFeedback.setFbAssignState(0);
        GridCityDto gridCityDto = aqiMeasuredResultFeign.getGridCityById(aqiFeedback.getCityId());
        String cityName = gridCityDto.getCityName();
        String provinceName = gridCityDto.getProvinceName();
        aqiFeedback.setFbAddress(provinceName + cityName + aqiFeedback.getFbAddress());
        return CommonResult.success(aqiFeedbackService.createAqiFeedback(aqiFeedback));
    }

    @RequestMapping(value = "/assignAqiFeedback",method = RequestMethod.POST )
    @ApiOperation(value = "指派反馈", notes = "指派反馈")
    public CommonResult<Integer> assignAqiFeedback(@RequestParam("fbId") Integer fbId,
                                                   @RequestParam("insId") Integer insId) {
        return CommonResult.success(aqiFeedbackService.assignAqiFeedback(fbId, insId));
    }

    @RequestMapping(value = "/getAqiFeedbackById",method = RequestMethod.POST )
    @ApiOperation(value = "获取反馈详情", notes = "获取反馈详情")
    public CommonResult<AqiFeedback> getAqiFeedbackById(@RequestParam(value = "fbId")Integer fbId)  {
        AqiFeedback aqiFeedback = aqiFeedbackService.getAqiFeedbackById(fbId);
        return CommonResult.success(aqiFeedback);
    }
}
